import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const router = new Router({
    routes: [

        // { path: '/', component: () => import('@/components/star.vue') },
        { path: '/', redirect: '/login' },
        {
            path: '/login',
            component: () => import('@/components/Login.vue')
        },
        {
            path: '/home',
            name: "home",
            redirect: "/welcome",
            component: () => import('@/components/Home.vue'),
            children: [{
                path: "/welcome",
                component: () => import('@/components/Welcome.vue')
            },
            {
                path: "/users",
                component: () => import('@/components/user/User.vue')
            },
            {
                path: "/rights",
                component: () => import('@/components/power/Rights.vue')
            },
            {
                path: "/roles",
                component: () => import('@/components/power/Roles.vue')
            },
            {
                path: "/categories",
                component: () => import('@/components/goods/Cate.vue')
            },
            {
                path: "/goods",
                component: () => import('@/components/goods/Goods.vue')
            }, {
                path: "/params",
                component: () => import('@/components/goods/Params.vue')
            },
            {
                path: "/orders",
                component: () => import('@/components/order/Order.vue')
            },
            {
                path: "/goods/add",
                component: () => import('@/components/goods/Add.vue')
            },
            {
                path: "/reports",
                component: () => import('@/components/report/Report.vue')
            }
            ]
        }
    ]

})



//挂载路由导航守卫


router.beforeEach((to, from, next) => {
    // to 将要访问的路径
    // from 代表从哪个路径跳转而来
    // next 是一个函数表示放行
    // next() 放行   next('/login') 强制跳转的路径


    if (to.path === '/login') return next()
    //获取token
    const token = window.sessionStorage.getItem('token')
    //判断token是否存在  不存在直接墙绘跳转登录页面  
    if (!token) return next('/login')
    // 存在直接放行
    next()


    //退出直接清空token
    // window.sessionStorage.clear()
    // this.$router.push('/login')


})



export default router